package com.fc.test.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import com.fc.test.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.IdGenerator;

/**
 * Service
* @Title: TAbbJquanchengService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-15 15:47:57  
 */
@Service
public class TAbbJquanchengService implements BaseService<TAbbJquancheng, TAbbJquanchengExample>{
	@Autowired
	private TAbbJquanchengMapper tAbbJquanchengMapper;

	@Autowired
	private TAbbMemberMapper memberMapper;

	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;

/*	@Autowired
	private TAbbMemberServcie tAbbMemberServcie;*/

	@Autowired
	private TAbbQuanchengMapper tAbbQuanchengMapper;
	@Autowired
	private TAbbCommissionMapper commissionMapper;

	@Autowired
	private TAbbQczifeiMapper qczifeiMapper;

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TAbbJquancheng> list(Tablepar tablepar,String name,Integer status){
	        TAbbJquanchengExample testExample=new TAbbJquanchengExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andTitleLike("%"+name+"%");
	        }
			 if(status!=null && status!=0){
				 testExample.createCriteria().andStatusEqualTo(status);
			 }else{
				 testExample.createCriteria().andStatusEqualTo(2);
			 }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TAbbJquancheng> list= tAbbJquanchengMapper.selectByExample(testExample);
	        PageInfo<TAbbJquancheng> pageInfo = new PageInfo<TAbbJquancheng>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		TAbbJquanchengExample example=new TAbbJquanchengExample();
		example.createCriteria().andIdIn(lista);
		return tAbbJquanchengMapper.deleteByExample(example);
	}


	@Override
	public TAbbJquancheng selectByPrimaryKey(String id) {

		return tAbbJquanchengMapper.selectByPrimaryKey(id);
	}


	@Override
	@Transactional
	public int updateByPrimaryKeySelective(TAbbJquancheng record) {


		//审核未通过
		/*if (record.getStatus()==3){

		}*/
		//审核通过
		if (record.getStatus()==4){
			TAbbJquancheng tAbbJquancheng=tAbbJquanchengMapper.selectByPrimaryKey(record.getId());//回复的任务的实体
			TAbbQuancheng tAbbQuancheng=tAbbQuanchengMapper.selectByPrimaryKey(tAbbJquancheng.getQcid());//发布全城任务实体
			TAbbMember tAbbMember=tAbbMemberMapper.selectByPrimaryKey(tAbbJquancheng.getUserid());//回复用户的实体

			//用户账户加上佣金
			TAbbMember tAbbMember3=new TAbbMember();
			tAbbMember3.setId(tAbbMember.getId());
			double comission=tAbbJquancheng.getJlblance();
			tAbbMember3.setAccount(tAbbMember.getAccount()+comission);
			tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember3);
           //用户佣金记录表加上佣金
			TAbbCommission tAbbCommission3=new TAbbCommission();
			tAbbCommission3.setCreattime(DateUtil.getCurrentDateTimeStr());
			tAbbCommission3.setType(1);
			tAbbCommission3.setBeiyongone(String.valueOf(comission));
			tAbbCommission3.setBusinessid("1");
			tAbbCommission3.setMemberid(tAbbMember.getId());
			tAbbCommission3.setMessage("任务奖励");
			tAbbCommission3.setId(SnowflakeIdWorker.getUUID());
			commissionMapper.insertSelective(tAbbCommission3);
		}
		return tAbbJquanchengMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbJquancheng record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbJquanchengMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbJquancheng record, TAbbJquanchengExample example) {

		return tAbbJquanchengMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbJquancheng record, TAbbJquanchengExample example) {

		return tAbbJquanchengMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbJquancheng> selectByExample(TAbbJquanchengExample example) {

		return tAbbJquanchengMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbJquanchengExample example) {

		return tAbbJquanchengMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbJquanchengExample example) {

		return tAbbJquanchengMapper.deleteByExample(example);
	}


	public List<TAbbJquancheng> getQcRecords(Map<String,Object> paramMap){
		return tAbbJquanchengMapper.getQcRecords(paramMap);
	}

	@Transactional
	public int deleteQcRecords(String id){
		TAbbJquancheng jquancheng = tAbbJquanchengMapper.selectByPrimaryKey(id);
		String qcid = jquancheng.getQcid();
		TAbbQuancheng quancheng = tAbbQuanchengMapper.selectByPrimaryKey(qcid);
		DecimalFormat df = new DecimalFormat("#.00");
		//计算佣金金额
		TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
		double money = Double.parseDouble(qczifei.getMoney());
		double pingtaiscore = Double.parseDouble(qczifei.getPingtaiscore());
		double yongjin = money*((100-pingtaiscore)/100);

		//修改全城任务的任务佣金和剩余名额
		quancheng.setBalance(Double.parseDouble(df.format(quancheng.getBalance()+yongjin)));
		quancheng.setRealnumber(quancheng.getRealnumber()+1);
		tAbbQuanchengMapper.updateByPrimaryKey(quancheng);

		return tAbbJquanchengMapper.deleteByPrimaryKey(id);
	}


	@Transactional
	public Map<String,Object> receiveQuancheng(String qcid,String userid){
		Map<String,Object> resMap = new HashMap<>();
		Map<String,Object> paramMap = new HashMap<>();
		DecimalFormat df = new DecimalFormat("#.00");
		paramMap.put("qcid",qcid);
		paramMap.put("userid",userid);
		int countToday = tAbbJquanchengMapper.isReceivedToday(paramMap);
		int notSubmit = tAbbJquanchengMapper.isHaveNotSubmit(paramMap);
		if(countToday>0){
			resMap.put("success",false);
			resMap.put("msg","今天已经领取过了");
		}else if(notSubmit>0){
			resMap.put("success",false);
			resMap.put("msg","还有任务未提交，提交后再领");
		}else{
			//计算佣金金额
			TAbbQczifei qczifei = qczifeiMapper.SelectLimitone();
			double money = Double.parseDouble(qczifei.getMoney());
			double pingtaiscore = Double.parseDouble(qczifei.getPingtaiscore());
			double yongjin = money*((100-pingtaiscore)/100);

			TAbbQuancheng tAbbQuancheng = tAbbQuanchengMapper.selectByPrimaryKey(qcid);
			TAbbMember member = memberMapper.selectByPrimaryKey(userid);
			TAbbJquancheng tAbbJquancheng = new TAbbJquancheng();
			tAbbJquancheng.setId(SnowflakeIdWorker.getUUID());
			tAbbJquancheng.setCreattime(DateUtil.getCurrentDateTimeStr());
			if(null!=tAbbQuancheng.getUsername()&&!"".equals(tAbbQuancheng.getUsername())){
				tAbbJquancheng.setFbusername(member.getNicename());
			}else{
				tAbbJquancheng.setFbusername(member.getNicename());
			}
			tAbbJquancheng.setUserid(userid);
			tAbbJquancheng.setQcid(qcid);
			tAbbJquancheng.setTitle(tAbbQuancheng.getTitle());
			tAbbJquancheng.setStatus(1);
			tAbbJquancheng.setJlblance(yongjin);
			int flag = tAbbJquanchengMapper.insert(tAbbJquancheng);

			//修改全城任务的任务佣金的剩余名额
			tAbbQuancheng.setBalance(Double.parseDouble(df.format(tAbbQuancheng.getBalance()-yongjin)));
			tAbbQuancheng.setRealnumber(tAbbQuancheng.getRealnumber()-1);
			tAbbQuanchengMapper.updateByPrimaryKey(tAbbQuancheng);

			if(flag>0){
				resMap.put("success",true);
				resMap.put("msg","任务领取成功");
				resMap.put("jdId",tAbbJquancheng.getId());
			}else{
				resMap.put("success",false);
				resMap.put("msg","领取失败，稍后再试");
			}
		}
		return resMap;
	}

	public Map<String,Object> submitQuancheng(String jdId,String images){
		Map<String,Object> resMap = new HashMap<>();
		TAbbJquancheng tAbbJquancheng = tAbbJquanchengMapper.selectByPrimaryKey(jdId);
		TAbbQuancheng quancheng = tAbbQuanchengMapper.selectByPrimaryKey(tAbbJquancheng.getQcid());
		String createTime = tAbbJquancheng.getCreattime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date rs = sdf.parse(createTime);
			Calendar c = Calendar.getInstance();
			long now = c.getTimeInMillis();
			long lastly = rs.getTime();

			if((now - lastly) < 1800000){
				resMap.put("success",false);
				resMap.put("msg","领取任务30分钟后才可以提交");
			}else{
				/*if(quancheng.getRealnumber()==0){
					resMap.put("success",false);
					resMap.put("msg","此任务已结束，换个别的任务吧");
				}else{*/
					tAbbJquancheng.setStatus(2);
					tAbbJquancheng.setImages(images);
					int flag = tAbbJquanchengMapper.updateByPrimaryKey(tAbbJquancheng);
					if(flag>0){
						resMap.put("success",true);
						resMap.put("msg","提交成功");
					}else{
						resMap.put("success",false);
						resMap.put("msg","提交失败");
					}
				/*}*/
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resMap;
	}


    public int yijian(List ids) {

		//审核通过
		for (Object id:ids){
			TAbbJquancheng tAbbJquancheng=tAbbJquanchengMapper.selectByPrimaryKey((String) id);//回复的任务的实体
			TAbbQuancheng tAbbQuancheng=tAbbQuanchengMapper.selectByPrimaryKey(tAbbJquancheng.getQcid());//发布全城任务实体
			TAbbMember tAbbMember=tAbbMemberMapper.selectByPrimaryKey(tAbbJquancheng.getUserid());//回复用户的实体

			//用户账户加上佣金
			TAbbMember tAbbMember3=new TAbbMember();
			tAbbMember3.setId(tAbbMember.getId());
			double comission=tAbbJquancheng.getJlblance();
			tAbbMember3.setAccount(tAbbMember.getAccount()+comission);
			tAbbMemberMapper.updateByPrimaryKeySelective(tAbbMember3);
			//用户佣金记录表加上佣金
			TAbbCommission tAbbCommission3=new TAbbCommission();
			tAbbCommission3.setCreattime(DateUtil.getCurrentDateTimeStr());
			tAbbCommission3.setType(1);
			tAbbCommission3.setBeiyongone(String.valueOf(comission));
			tAbbCommission3.setBusinessid("1");
			tAbbCommission3.setMemberid(tAbbMember.getId());
			tAbbCommission3.setMessage("任务奖励");
			tAbbCommission3.setId(SnowflakeIdWorker.getUUID());
			commissionMapper.insertSelective(tAbbCommission3);
		}
		return tAbbJquanchengMapper.yijian(ids);
    }
}
